﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace B2B.Models
{
    public class PostBuyRequest
    {
        int maSanPhamMua;

        public int MaSanPhamMua
        {
            get { return maSanPhamMua; }
            set { maSanPhamMua = value; }
        }
        string tenSanPham;

        public string TenSanPham
        {
            get { return tenSanPham; }
            set { tenSanPham = value; }
        }
        int maDanhMuc;

        public int MaDanhMuc
        {
            get { return maDanhMuc; }
            set { maDanhMuc = value; }
        }
        int soLuongCanMua;

        public int SoLuongCanMua
        {
            get { return soLuongCanMua; }
            set { soLuongCanMua = value; }
        }
        string hinhAnh;

        public string HinhAnh
        {
            get { return hinhAnh; }
            set { hinhAnh = value; }
        }
        int maThanhVien;

        public int MaThanhVien
        {
            get { return maThanhVien; }
            set { maThanhVien = value; }
        }
        string chiTiet;

        public string ChiTiet
        {
            get { return chiTiet; }
            set { chiTiet = value; }
        }
        int tinhTrang;

        public int TinhTrang
        {
            get { return tinhTrang; }
            set { tinhTrang = value; }
        }
        private DateTime ngayDang;

        public DateTime NgayDang
        {
            get { return ngayDang; }
            set { ngayDang = value; }
        }
        string tenDanhMuc;

        public string TenDanhMuc
        {
            get { return tenDanhMuc; }
            set { tenDanhMuc = value; }
        }

        string hoten;

        public string Hoten
        {
            get { return hoten; }
            set { hoten = value; }
        }

        string email;

        public string Email
        {
            get { return email; }
            set { email = value; }
        }

        int maLinhVuc;

        public int MaLinhVuc
        {
            get { return maLinhVuc; }
            set { maLinhVuc = value; }
        }

        string tenLinhVuc;

        public string TenLinhVuc
        {
            get { return tenLinhVuc; }
            set { tenLinhVuc = value; }
        }


        

      


        public int ThemSanPhamMua(PostBuyRequest id, string TenHinh)
        {
            try
            {
                if (id.TenSanPham == null)
                {
                    return 2;
                }
                SqlConnection conn = KetNoi.Connection();
                SqlCommand cmd = new SqlCommand("SP_THEMSANPHAMMUA", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@ChiTiet", id.ChiTiet);
                if (id.HinhAnh == null)
                {
                    id.HinhAnh = "";
                }
                cmd.Parameters.AddWithValue("@HinhAnh", TenHinh);
                cmd.Parameters.AddWithValue("@MaDanhMuc", id.MaDanhMuc);
                cmd.Parameters.AddWithValue("@MaThanhVien", id.MaThanhVien);
                cmd.Parameters.AddWithValue("@SoLuongCanMua", id.SoLuongCanMua);
                cmd.Parameters.AddWithValue("@TenSP", id.TenSanPham);
                
                conn.Open();
                int kq = cmd.ExecuteNonQuery();
                conn.Close();
                return 1;
            }
            catch (Exception e)
            {
                string kq = e.ToString();
                return 0;
            }
            
        }

        public ArrayList DanhSachSanPhamMua(int DongBatDau)
        {
            ArrayList list = new ArrayList();
            SqlConnection conn = KetNoi.Connection();
            SqlCommand cmd = new SqlCommand("SP_DANHSACHSANPHAMMUA_2", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@PageCurent", DongBatDau);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                PostBuyRequest spm = new PostBuyRequest();
                spm.MaSanPhamMua = reader.GetInt32(0);
                spm.TenSanPham = reader.GetString(1);
                spm.MaDanhMuc = reader.GetInt32(2);
                spm.SoLuongCanMua = reader.GetInt32(3);
                spm.HinhAnh = reader.GetString(4);
                spm.MaThanhVien = reader.GetInt32(5);
                spm.ChiTiet = reader.GetString(6);
                spm.TinhTrang = reader.GetInt32(7);
                spm.NgayDang = reader.GetDateTime(8);
                spm.TenDanhMuc = reader.GetString(9);
                spm.Hoten = reader.GetString(10);
                spm.Email = reader.GetString(11);
                list.Add(spm);
            }
            conn.Close();
            return list;
        }

        public int TongSanPhamTheoMa(int id)
        {
            SqlConnection conn = KetNoi.Connection();
            SqlCommand cmd = new SqlCommand("SP_TONGSPMUATHEOMATV", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@MaTV", id);
            conn.Open();
            int totalRow = int.Parse(cmd.ExecuteScalar().ToString());
            conn.Close();
            return totalRow;
        }

        public int XoaSanPhamMua(int id)
        {
            try
            {
                SqlConnection conn = KetNoi.Connection();
                SqlCommand cmd = new SqlCommand("SP_XOASANPHAMMUA", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@MaSPMua", id);
                conn.Open();
                int totalRow = int.Parse(cmd.ExecuteNonQuery().ToString());
                conn.Close();
                return totalRow;
            }
            catch (Exception ex)
            {
                return 0;
            }
           
        }

        public int CapNhatSanPhamMua(PostBuyRequest id, string TenHinh, int MaSanPhamMua)
        {
            try
            {
                if (id.TenSanPham == null)
                {
                    return 2;
                }
                SqlConnection conn = KetNoi.Connection();
                SqlCommand cmd = new SqlCommand("SP_CAPNHATSANPHAMMUA", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                
                if (id.HinhAnh == null)
                {
                    id.HinhAnh = "";
                }
                cmd.Parameters.AddWithValue("@ChiTiet", id.ChiTiet);
                cmd.Parameters.AddWithValue("@HinhAnh", TenHinh);
                cmd.Parameters.AddWithValue("@MaDM", id.MaDanhMuc);
                cmd.Parameters.AddWithValue("@MaTV", id.MaThanhVien);
                cmd.Parameters.AddWithValue("@SLCM", id.SoLuongCanMua);
                cmd.Parameters.AddWithValue("@TenSP", id.TenSanPham);
                cmd.Parameters.AddWithValue("@TinhTrang", id.TinhTrang);
                cmd.Parameters.AddWithValue("@MaSPMua", MaSanPhamMua);
                conn.Open();
                int kq = cmd.ExecuteNonQuery();
                conn.Close();
                return 1;
            }
            catch (Exception e)
            {
                string kq = e.ToString();
                return 0;
            }
        }
        public PostBuyRequest LaySanPhamMua(int id)
        {
            PostBuyRequest spm = new PostBuyRequest();
            SqlConnection conn = KetNoi.Connection();
            SqlCommand cmd = new SqlCommand("SP_LAYSANPHAMMUA", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@MaSPMua", id);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {                
                spm.MaSanPhamMua = reader.GetInt32(0);
                spm.TenSanPham = reader.GetString(1);
                spm.MaDanhMuc = reader.GetInt32(2);
                spm.SoLuongCanMua = reader.GetInt32(3);
                spm.HinhAnh = reader.GetString(4);
                spm.MaThanhVien = reader.GetInt32(5);
                spm.ChiTiet = reader.GetString(6);
                spm.TinhTrang = reader.GetInt32(7);
                spm.NgayDang = reader.GetDateTime(8);
                spm.TenDanhMuc = reader.GetString(9);
                spm.MaLinhVuc = reader.GetInt32(10);
                spm.TenLinhVuc = reader.GetString(11);
            }
            conn.Close();
            return spm;
        }

        public ArrayList LaySanPhamMuaTheoMa(int id)
        {
            ArrayList list = new ArrayList();
            SqlConnection conn = KetNoi.Connection();
            SqlCommand cmd = new SqlCommand("SP_LAYSANPHAMMUA", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@MaSPMua", id);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                PostBuyRequest spm = new PostBuyRequest();
                 spm.MaSanPhamMua = reader.GetInt32(0);
                spm.TenSanPham = reader.GetString(1);
                spm.MaDanhMuc = reader.GetInt32(2);
                spm.SoLuongCanMua = reader.GetInt32(3);
                spm.HinhAnh = reader.GetString(4);
                spm.MaThanhVien = reader.GetInt32(5);
                spm.ChiTiet = reader.GetString(6);
                spm.TinhTrang = reader.GetInt32(7);
                spm.NgayDang = reader.GetDateTime(8);
                spm.TenDanhMuc = reader.GetString(9);
                list.Add(spm);
            }
            conn.Close();
            return list;
        }

        public int CapNhatTinhTrang(int id, int TinhTrang)
        {
            int MaSanPhamMua = id;
            try
            {
                SqlConnection conn = KetNoi.Connection();
                SqlCommand cmd = new SqlCommand("SP_CAPNHATTINHTRANGSPMUA", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@TinhTrang", TinhTrang);
                cmd.Parameters.AddWithValue("@MaSPMua", MaSanPhamMua);
                conn.Open();
                int kq = cmd.ExecuteNonQuery();
                conn.Close();
                return 1;
            }
            catch (Exception e)
            {
                string kq = e.ToString();
                return 0;
            }
        }

        public int TongSanPham()
        {
            SqlConnection conn = KetNoi.Connection();
            SqlCommand cmd = new SqlCommand("SP_TONGSPMUA", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            int totalRow = int.Parse(cmd.ExecuteScalar().ToString());
            conn.Close();
            return totalRow;
        }

        public ArrayList DanhSachSanPhamMua_MaTV(int maTV, int page)
        {
            ArrayList list = new ArrayList();
            SqlConnection conn = KetNoi.Connection();
            SqlCommand cmd = new SqlCommand("SP_DANHSACHSANPHAMMUA", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@MaTV", maTV);
            cmd.Parameters.AddWithValue("@PAGECURENT", page);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                PostBuyRequest spm = new PostBuyRequest();
                spm.MaSanPhamMua = reader.GetInt32(0);
                spm.TenSanPham = reader.GetString(1);
                spm.MaDanhMuc = reader.GetInt32(2);
                spm.SoLuongCanMua = reader.GetInt32(3);
                spm.HinhAnh = reader.GetString(4);
                spm.MaThanhVien = reader.GetInt32(5);
                spm.ChiTiet = reader.GetString(6);
                spm.TinhTrang = reader.GetInt32(7);
                spm.NgayDang = reader.GetDateTime(8);
                spm.TenDanhMuc = reader.GetString(9);
                list.Add(spm);
            }
            conn.Close();
            return list;
        }

        public ArrayList DanhSachSanPhamMua_Admin(int page)
        {
            ArrayList list = new ArrayList();
            SqlConnection conn = KetNoi.Connection();
            SqlCommand cmd = new SqlCommand("SP_DANHSACHSANPHAMMUA_ADMIN", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@PAGECURENT", page);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                PostBuyRequest spm = new PostBuyRequest();
                spm.MaSanPhamMua = reader.GetInt32(0);
                spm.TenSanPham = reader.GetString(1);
                spm.MaDanhMuc = reader.GetInt32(2);
                spm.SoLuongCanMua = reader.GetInt32(3);
                spm.HinhAnh = reader.GetString(4);
                spm.MaThanhVien = reader.GetInt32(5);
                spm.ChiTiet = reader.GetString(6);
                spm.TinhTrang = reader.GetInt32(7);
                spm.NgayDang = reader.GetDateTime(8);
                spm.TenDanhMuc = reader.GetString(9);
                list.Add(spm);
            }
            conn.Close();
            return list;
        }

        public int tongSoTrangPostBuy(int matv)
        {
            int tongSP = 0;
            SqlConnection conn = KetNoi.Connection();
            SqlCommand cmd = new SqlCommand("SP_TONGSANPHAMMUA_THANHVIEN", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@MATV", matv);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                tongSP = reader.GetInt32(0);
            }
            int tongTrang = tongSP / 10;
            if (tongSP % 10 != 0)
                tongTrang++;
            return tongTrang;
        }

       
    }
}